<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="请输入类别关键字" v-model="search.name">
      </el-input>
      <el-button class="filter-item" type="primary" plain v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" v-roles="'editCategory'" @click="editBtn()" v-waves  type="primary" icon="el-icon-edit">添加主类栏目</el-button>
    </div>

    <el-table :data="listData" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">
      <el-table-column type="index"  align="center" label="序号" width="80"></el-table-column>
      <el-table-column min-width="120px" label="栏目名称">
        <template slot-scope="scope">
          <span>{{scope.row.name}}</span>
        </template>
      </el-table-column>

      <el-table-column class-name="status-col" label="状态" width="100">
        <template slot-scope="scope">
           <el-tag :type="scope.row.status | statusFilter">{{scope.row.status | statusName}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.status==2" size="mini" @click="handleModifyStatus(scope.row,'1')">开启</el-button>
          <el-button v-if="scope.row.status==1" size="mini" @click="handleModifyStatus(scope.row,'2')">关闭</el-button>
          <el-button type="primary" size="mini" @click="editBtn(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handlDel(scope.row)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

    <!-- 弹框添加/修改-->
    <el-dialog :title="eidtlm" :visible.sync="editVisible">
        <el-form :rules="rules" ref="tempForm" :model="addTemp" label-position="left" label-width="80px" style='width: 400px; margin-left:60px;'  class="demo-ruleForm">
          <el-form-item label="栏目名称" prop="name">
            <el-input  class="filter-item" placeholder="商品类别名称" v-model="addTemp.name"></el-input>
          </el-form-item>
          <el-form-item label="状态" prop="status">
          <el-radio-group v-model="addTemp.status">
            <el-radio label="2">关闭栏目</el-radio>
            <el-radio label="1">开启栏目</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="addLoading" @click="handleUpdate">保存</el-button>
      </span>
    </el-dialog>
    <!-- 弹框添加  end -->

    <!--可自定义按钮的样式、show/hide临界点、返回的位置  -->
    <!--如需文字提示，可在外部添加element的<el-tooltip></el-tooltip>元素  -->
    <el-tooltip placement="top" content="回到顶部">
      <back-to-top transitionName="fade" :customStyle="myBackToTopStyle" :visibilityHeight="300" :backPosition="50"></back-to-top>
    </el-tooltip>
  </div>
</template>
<script>
import waves from '@/directive/waves' // 水波纹指令
import BackToTop from '@/components/BackToTop' // 回到顶部
import * as api from '@/api/main-api' // API
import { mapGetters } from 'vuex'

export default {
  name: 'Category',
  directives: {
    waves
  },
  components: { BackToTop },

  data() {
    return {

      // 回到顶部样式
      myBackToTopStyle: {
        right: '50px',
        bottom: '50px',
        width: '40px',
        height: '40px',
        'border-radius': '4px',
        'line-height': '45px', // 请保持与高度一致以垂直居中
        background: '#e7eaf1'// 按钮的背景颜色
      },
      listQuery: {
        page: 1,
        pagesize: 10
      },
      // 搜索
      search: {
        name: ''
      },
      addTemp: {
        name: '',
        status: '1', // 默认显示
        unit_id: '0'
      },
      eidtlm: '添加栏目',
      // 规则
      rules: {
        name: [
          { required: true, message: '请输入栏目名称', trigger: 'blur' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符', trigger: 'blur' }
        ]
      },
      listData: [],
      listLoading: false,
      addLoading: false,
      editVisible: false,
      total: 0
    }
  },

  // 装换属性
  filters: {
    statusFilter(status) {
      const statusMap = {
        0: 'info',
        1: 'success',
        2: 'danger'
      }
      return statusMap[status]
    },
    statusName(status) {
      return (status === '1') ? '显示中' : (status === '2') ? '关闭' : '未知'
    }
  },

  computed: {
    ...mapGetters(['outlet_id'])
  },

  // 初始化数据
  created() {
    setTimeout(() => {
      this.fetchData()
    }, 20)
  },

  methods: {
    // 获取数据
    fetchData() {
      this.listLoading = true
      this.listQuery = Object.assign(this.listQuery, this.search)
      return new Promise((resolve, reject) => {
        api.maincategoriesApi(this.listQuery).then(response => {
          if (response.data.code === 0) {
            this.listData = response.data.data
            this.total = parseInt(response.data.total)
          }
          this.listLoading = false
        })
      })
    },

    // 搜索
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },

    // 编辑弹框-按钮
    editBtn(row) {
      this.editVisible = true
      if (typeof (row) === 'undefined') { // 新增
        this.eidtlm = '添加栏目'
      } else { // 修改
        this.eidtlm = '编辑栏目'
        this.addTemp.id = row.id
        this.addTemp.name = row.name
        this.addTemp.status = row.status
      }
    },

    // 修改状态
    handleModifyStatus(row, status) {
      var data = {
        id: row.id,
        unit_id: '1000', // 添加属性
        field: 'status', // 修改单个字段
        status: status, // 值
        types: 'category'
      }
      this.postFieldStatus(data, row)
      row.status = data.status
    },

    // 添加/编辑-栏目
    handleUpdate() {
      this.$refs.tempForm.validate(valid => {
        if (valid) {
          this.addLoading = true
          api.editCategoryApi(this.addTemp).then(response => {
            if (response.data.code === 0) {
              this.fetchData()
              this.$root.message('操作成功', 'success')
            }
          })
          this.editVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
        this.addLoading = false
      })
    },

    // 删除操作
    handlDel(row) {
      this.$confirm('此操作将永久删除, 商品将全部下架是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var data = {
          id: row.id,
          unit_id: '0' // 添加属性
        }
        api.delCategoryApi(data).then(response => {
          if (response.data.code === 0) {
            this.$root.message('删除成功', 'success')
            const index = this.listData.indexOf(row)
            this.listData.splice(index, 1)
          }
        })
      }).catch(() => {
        this.$root.message('已取消删除', 'info')
      })
    },

    // 改变页条数
    handleSizeChange(val) {
      this.listQuery.pagesize = val
      this.fetchData()
    },

    // 跳页
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchData()
    },

    // 改变总状态02版(统一)
    postFieldStatus(data, row) {
      api.stateApi(data).then(response => {
        if (response.data.code === 0) {
          this.$root.message('操作成功', 'success')
        }
      })
    }

  }

}
</script>
